package com.edu.exam.entity.assessment.technical;

import com.edu.exam.enums.common.DataStatus;
import com.edu.exam.enums.common.SourceModule;
import com.edu.exam.enums.assessment.technical.*;
import jakarta.persistence.*;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.Accessors;
import org.hibernate.annotations.CreationTimestamp;
import org.hibernate.annotations.UpdateTimestamp;

import java.time.LocalDateTime;

/**
 * 质量数据实体类
 * 所属模块：B-02 题库管理模块
 * 对应数据库表：quality_data
 *
 * 根据E-06-03质量监控子模块设计实现
 * 对应quality_data数据表，用于存储来自E-03和E-04模块的原始质量数据
 *
 * @author System
 * @version 1.0.0
 */
@Entity
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@Accessors(chain = true)
public class QualityData {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id")
    private Long id;

    @Column(name = "data_id", length = 32, unique = true, nullable = false)
    private String dataId;

    @Enumerated(EnumType.STRING)
    @Column(name = "source_module", length = 20, nullable = false)
    private SourceModule sourceModule;

    @Column(name = "task_id", length = 32, nullable = false)
    private String taskId;

    @Column(name = "exam_session_id", length = 32)
    private String examSessionId;

    @Column(name = "scan_batch_id", length = 32)
    private String scanBatchId;

    @Column(name = "student_id", length = 32, nullable = false)
    private String studentId;

    @Column(name = "question_id", length = 32, nullable = false)
    private String questionId;

    @Column(name = "grader_id", length = 32, nullable = false)
    private String graderId;

    @Column(name = "grading_metrics", columnDefinition = "JSON", nullable = false)
    private String gradingMetrics;

    @Column(name = "security_metrics", columnDefinition = "JSON")
    private String securityMetrics;

    @Column(name = "scanning_metrics", columnDefinition = "JSON")
    private String scanningMetrics;

    @Column(name = "raw_data", columnDefinition = "JSON")
    private String rawData;

    @Enumerated(EnumType.STRING)
    @Column(name = "data_status", length = 20, nullable = false)
    private DataStatus dataStatus;

    @Column(name = "received_at", nullable = false)
    private LocalDateTime receivedAt;

    @Column(name = "processed_at")
    private LocalDateTime processedAt;

    // 扩展字段
    @Column(name = "submission_id", length = 32)
    private String submissionId; // 提交ID

    @Column(name = "paper_id", length = 32)
    private String paperId; // 试卷ID

    @Column(name = "question_type", length = 20)
    private String questionType; // 题目类型

    @Column(name = "question_difficulty", length = 20)
    private String questionDifficulty; // 题目难度

    @Column(name = "subject_id", length = 32)
    private String subjectId; // 科目ID

    @Column(name = "exam_id", length = 32)
    private String examId; // 考试ID

    @Column(name = "session_id", length = 32)
    private String sessionId; // 会话ID

    @Column(name = "batch_id", length = 32)
    private String batchId; // 批次ID

    @Column(name = "grader_name", length = 100)
    private String graderName; // 批阅人姓名

    @Column(name = "grader_department", length = 50)
    private String graderDepartment; // 批阅人部门

    @Column(name = "grader_experience_level", length = 20)
    private String graderExperienceLevel; // 批阅人经验等级

    @Column(name = "grading_duration_seconds")
    private Integer gradingDurationSeconds; // 批阅耗时（秒）

    @Column(name = "grading_start_time")
    private LocalDateTime gradingStartTime; // 批阅开始时间

    @Column(name = "grading_end_time")
    private LocalDateTime gradingEndTime; // 批阅结束时间

    @Column(name = "score_awarded", precision = 5, scale = 2)
    private Double scoreAwarded; // 给予分数

    @Column(name = "max_score", precision = 5, scale = 2)
    private Double maxScore; // 满分

    @Column(name = "score_percentage", precision = 5, scale = 2)
    private Double scorePercentage; // 得分百分比

    @Column(name = "grading_confidence", precision = 3, scale = 2)
    private Double gradingConfidence; // 批阅置信度

    @Column(name = "grading_method", length = 20)
    private String gradingMethod; // 批阅方式

    @Column(name = "review_required")
    private Boolean reviewRequired; // 是否需要审查

    @Column(name = "review_status", length = 20)
    private String reviewStatus; // 审查状态

    @Column(name = "reviewed_by", length = 32)
    private String reviewedBy; // 审查人

    @Column(name = "review_time")
    private LocalDateTime reviewTime; // 审查时间

    @Column(name = "review_comments", columnDefinition = "TEXT")
    private String reviewComments; // 审查意见

    @Column(name = "quality_flags", columnDefinition = "JSON")
    private String qualityFlags; // 质量标记

    @Column(name = "anomaly_indicators", columnDefinition = "JSON")
    private String anomalyIndicators; // 异常指标

    @Column(name = "processing_errors", columnDefinition = "TEXT")
    private String processingErrors; // 处理错误

    @Column(name = "validation_status", length = 20)
    private String validationStatus; // 验证状态

    @Column(name = "validation_errors", columnDefinition = "TEXT")
    private String validationErrors; // 验证错误

    @Column(name = "data_quality_score", precision = 5, scale = 2)
    private Double dataQualityScore; // 数据质量评分

    @Column(name = "completeness_score", precision = 5, scale = 2)
    private Double completenessScore; // 完整性评分

    @Column(name = "accuracy_score", precision = 5, scale = 2)
    private Double accuracyScore; // 准确性评分

    @Column(name = "consistency_score", precision = 5, scale = 2)
    private Double consistencyScore; // 一致性评分

    @Column(name = "timeliness_score", precision = 5, scale = 2)
    private Double timelinessScore; // 及时性评分

    @Column(name = "fairness_score", precision = 5, scale = 2)
    private Double fairnessScore; // 公平性评分

    @Column(name = "overall_quality_score", precision = 5, scale = 2)
    private Double overallQualityScore; // 综合质量评分

    @Column(name = "quality_level", length = 20)
    private String qualityLevel; // 质量等级

    @Column(name = "data_source", length = 50)
    private String dataSource; // 数据来源

    @Column(name = "collection_method", length = 50)
    private String collectionMethod; // 采集方法

    @Column(name = "data_format", length = 20)
    private String dataFormat; // 数据格式

    @Column(name = "data_version", length = 10)
    private String dataVersion; // 数据版本

    @Column(name = "checksum", length = 64)
    private String checksum; // 数据校验和

    @Column(name = "encrypted")
    private Boolean encrypted; // 是否加密

    @Column(name = "archived")
    private Boolean archived; // 是否已归档

    @Column(name = "archive_time")
    private LocalDateTime archiveTime; // 归档时间

    @Column(name = "retention_days")
    private Integer retentionDays; // 保留天数

    @Column(name = "correlation_id", length = 100)
    private String correlationId; // 关联ID

    @Column(name = "parent_data_id", length = 32)
    private String parentDataId; // 父数据ID

    @Column(name = "child_data_ids", columnDefinition = "JSON")
    private String childDataIds; // 子数据ID列表

  

    // ===== 审计字段 =====

    /**
     * 创建时间
     */
    @CreationTimestamp
    @Column(name = "created_time", nullable = false, updatable = false)
    private LocalDateTime createdTime;

    /**
     * 更新时间
     */
    @UpdateTimestamp
    @Column(name = "updated_time", nullable = false)
    private LocalDateTime updatedTime;

    /**
     * 版本号（乐观锁）
     */
    @Version
    @Column(name = "version")
    private Integer version = 0;

    /**
     * 逻辑删除标记
     */
    @Column(name = "is_deleted", nullable = false)
    private Boolean isDeleted = false;
}
